/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package drt;

import evaluation.Edge;
import evaluation.Graph;
import evaluation.Vertex;
import java.util.LinkedHashSet;
import logic.Type;

/**
 *
 * @author phong
 */
public abstract class DRSBinaryComplexCondition extends DRSCondition {
    
    DrtExpression first, second;
    
    public DRSBinaryComplexCondition(DrtExpression first, DrtExpression second) {
        this.first = first;
        this.second = second;
        
        if (!first.getType().matches(Type.TRUTH_TYPE) || !second.getType().matches(Type.TRUTH_TYPE))
            throw new Error("incorrect type");
    }
        
    @Override
    public Type getType() {
        return Type.TRUTH_TYPE;
    }
    
    @Override
    public void getRefs(LinkedHashSet<DRSReferent> refs) {
        this.first.getRefs(refs);
        this.second.getRefs(refs);
    }
    
    @Override
    public void getAllVariables(LinkedHashSet<DrtVariable> vars) {
        this.first.getAllVariables(vars);
        this.second.getAllVariables(vars);
    }
    
    @Override
    public Graph toGraph() {
        
        Graph g1 = this.first.toGraph();
        Graph g2 = this.second.toGraph();
                
        Graph g = new Graph();
        Vertex p1 = new Vertex(Naming.getUniqueName("l0"), Vertex.TYPE_LABEL);
        Vertex p2 = new Vertex(Naming.getUniqueName("l0"), Vertex.TYPE_LABEL);
        
        g.addMarks(p1);
        g.addMarks(p2);
        
        for (Vertex v : g1.getMarks()) {
            g.addEdge(new Edge("", v, p1));
        }
        for (Vertex v : g2.getMarks()) {
            g.addEdge(new Edge("", v, p2));
        }
        
        Vertex opt = null;
        if (this instanceof DRSImpCondition) {
            opt = Vertex.createImp();
            g.addEdge(new Edge("1", p1, opt));
            g.addEdge(new Edge("2", p2, opt));
        }
        else if (this instanceof DRSOrCondition) {
            opt = Vertex.createOr();
            g.addEdge(new Edge("", p1, opt));
            g.addEdge(new Edge("", p2, opt));
        }
        else if (this instanceof DRSWhqCondition) {
            opt = Vertex.createWhq();
            g.addEdge(new Edge("1", p1, opt));
            g.addEdge(new Edge("2", p2, opt));
        }
        
        g.addMarks(opt);

        g.merge(g1);
        g.merge(g2);
        
        return g;
    }

    @Override
    public int hashCode() {
        return this.first.hashCode() * 31 + this.second.hashCode() * 17 + this.getClass().toString().hashCode();
    }
}
